package com.yyj.controller;

import com.github.pagehelper.PageInfo;
import com.yyj.entity.Employee;
import com.yyj.entity.Params;
import com.yyj.entity.Result;
import com.yyj.dao.EmployeeDao;
import com.yyj.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    // 在需要执行查询的地方注入EmployeeDao
    final
    EmployeeService employeeService;


    public EmployeeController(EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    //登录
    @PostMapping("/login")
    //请求实体
    public Result Login(@RequestBody Employee employee) {
        Employee loginUser = employeeService.login(employee);
        return Result.success(loginUser);
    }

    @PostMapping("/register")
    public Result Register(@RequestBody Employee employee) {
        employeeService.insertEmployee(employee);
        return Result.success();

    }

    //增加管理员信息
    @PostMapping
    public Result insertAdmin(@RequestBody Employee employee) {
        if (employee.getId() == null) {
            employeeService.insertEmployee(employee);
        } else {
            //修改
            employeeService.updateEmployee(employee);
        }
        return Result.success();
    }



    @GetMapping("/search")
    public Result getBySearch(Params params) {
        PageInfo<Employee> info = employeeService.getBySearch(params);
        return Result.success(info);
    }
    @GetMapping("/getPostIdOne")
    public Result getEmployeesWithPostIdOne(Params params) {
        PageInfo<Employee> info = employeeService.getEmployeesWithPostIdOne(params);
        return Result.success(info);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        employeeService.delete(id);
        return Result.success();
    }

    @GetMapping("/getInfo/{id}")
    public Result getInfo(@PathVariable Integer id) {
        Employee employee = employeeService.getInfo(id);
        return Result.success(employee);
    }

}
